<?php

class ContentsController extends \BaseController {

    protected $layout = 'layouts.admin';

    /**
     * Display a listing of contents
     *
     * @return Response
     */
    public function index() {
        $contents = Content::all();

        $this->layout->content = View::make('contents.index', compact('contents'));
    }

    /**
     * Show the form for creating a new content
     *
     * @return Response
     */
    public function create() {
        $this->layout->content = View::make('contents.create');
    }

    /**
     * Store a newly created content in storage.
     *
     * @return Response
     */
    public function store() {
        $content = new Content;

        if ($content->validate(Input::all())) {
            $topic = Topic::find(Input::get('topic_id'));
            $category = $topic->category()->first();
            $content->before_url = $this->saveImageFromUrl(Input::get('before_url') ,$category->slug , $topic->slug);
            $content->after_url = $this->saveImageFromUrl(Input::get('after_url') , $category->slug , $topic->slug);
            $content->setExistValues(array('topic_id' , 'year') , true);
            $content->save();
        }else {
            //return Redirect::back()->withErrors($content->errors)->withInput();
        }

        return Redirect::to('topics/edit/'.$content->topic_id);
    }

    /**
     * Display the specified content.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id) {
        $content = Content::findOrFail($id);

        return View::make('contents.show', compact('content'));
    }

    /**
     * Show the form for editing the specified content.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id) {
        $content = Content::find($id);

        $this->layout->content = View::make('contents.edit', compact('content'));
    }

    /**
     * Update the specified content in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id) {
        $content = Content::findOrFail($id);

        
        if ($content->validate(Input::all())) {
            $topic = $content->topic()->first();
            $category = $topic->category()->first();
            if(starts_with( Input::get('before_url'), 'http')){
                
                 $content->before_url = $this->saveImageFromUrl(Input::get('before_url') ,$category->slug , $topic->slug);
            }
            
            if(starts_with( Input::get('after_url'), 'http')){
                $content->after_url = $this->saveImageFromUrl(Input::get('after_url') , $category->slug , $topic->slug);
            }
            
            $content->year = Input::get('year');
            $content->save();
        }else {
            return Redirect::back()->withErrors($validator)->withInput();
        }

        //$content->update($data);

        return Redirect::to('topics/edit/'.$content->topic_id);
    }
    
    private function saveImageFromUrl($url , $category , $title){
            $content = file_get_contents($url);
            
            $name = $category.'-'.$title.'-'.uniqid().'_image.jpg';
            $fp = fopen('public/image/'.$name, "w");
            fwrite($fp, $content);
            fclose($fp);
            
            return $name;
    }

    /**
     * Remove the specified content from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id) {
       $content = Content::find($id)->with('topic');
        Content::destroy($id);
        
        return Redirect::route('topic/edit/'.$content->topic->id);
    }
    
    public function delete($id) {
        $content = Content::find($id)->with('topic');
        Content::destroy($id);
        
        
        return Redirect::route('topic/edit/'.$content->topic->id);
    }
}
